import { loginByEmail, logout, getInfo } from 'api/login';
import Cookies from 'js-cookie';
import sha256 from 'sha256';

const user = {
    state: {
        user: '',
        status: '',
        agentUsername: Cookies.get('agentUsername'),
        agentPassword: Cookies.get('agentPassword'),
        auserName: Cookies.get('auserName'),
        reportUserName: Cookies.get('reportUserName'), // 登陆之后的用户名
        code: '',
        uid: Cookies.get('uid'),
        authType: '',
        token: Cookies.get('accessToken'),
        name: '',
        avatar: '',
        introduction: '',
        agentType: Cookies.get('agentType'),
        roles: [],
        setting: {
            articlePlatform: []
        },
        roleId: Cookies.get('roleId'),
        menuGenerated: false
    },

    mutations: {
        SET_AUTH_TYPE: (state, type) => {
            state.authType = type;
        },
        SET_CODE: (state, code) => {
            state.code = code;
        },
        SET_TOKEN: (state, token) => {
            state.token = token;
        },
        SET_UID: (state, uid) => {
            state.uid = uid;
        },
        SET_INTRODUCTION: (state, introduction) => {
            state.introduction = introduction;
        },
        SET_SETTING: (state, setting) => {
            state.setting = setting;
        },
        SET_STATUS: (state, status) => {
            state.status = status;
        },
        SET_NAME: (state, name) => {
            state.name = name;
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar;
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles;
        },
        LOGIN_SUCCESS: () => {
            console.log('login success')
        },
        LOGOUT_USER: state => {
            state.user = '';
        },
        SET_AGENTUSERNAME: (state, username) => {
            state.agentUsername = username
        },
        SET_AGENTPASSWORD: (state, password) => {
            state.agentPassword = password;
        },
        SET_AGENTTYPE: (state, agentType) => {
            state.agentType = agentType;
        },
        SET_ROLEID: (state, roleId) => {
            state.roleId = roleId;
        },
        SET_MENU_GENERATED: (state, menuGenerated) => {
            state.menuGenerated = menuGenerated;
        },
        SET_USERNAME: (state, userName) => {
            state.userName = userName;
        },
        SET_REPORTUSERNAME: (state, reportUserName) => {
            state.reportUserName = reportUserName;
        }
    },

    actions: {

        LoginByUsername({ commit }, userInfo) {
            const username = userInfo.username.trim();
            const password = userInfo.password.trim();
            const pwd = sha256(password);
            return new Promise((resolve, reject) => {
                loginByEmail(username, pwd).then(response => {
                    const data = response.data.content;
                    Cookies.set('uid', data.userId);
                    Cookies.set('accessToken', data.accessToken);
                    Cookies.set('agentType', data.agentType);
                    Cookies.set('roleId', data.roleId);
                    Cookies.set('auserName', data.sAccount);
                    Cookies.set('reportUserName', data.userName);
                    commit('SET_USERNAME', data.account);
                    commit('SET_ROLEID', data.roleId);
                    commit('SET_UID', data.userId);
                    commit('SET_TOKEN', data.accessToken);
                    commit('SET_AGENTTYPE', data.agentType);
                    commit('SET_REPORTUSERNAME', data.userName);
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },

        // 记住用户名和密码
     /*    SetUserName({ commit }, userInfor) {
            if (userInfor.isRemberAccount) {
                Cookies.set('agentUsername', userInfor.username, 30);
                commit('SET_AGENTUSERNAME', userInfor.username);
            } else {
                Cookies.remove('agentUsername');
                commit('SET_AGENTUSERNAME', '');
            }
            if (userInfor.isRemberPassword) {
                Cookies.set('agentPassword', userInfor.password, 30);
                commit('SET_AGENTPASSWORD', userInfor.password);
            } else {
                Cookies.remove('agentPassword');
                commit('SET_AGENTPASSWORD', '');
            }
        }, */
        // 获取用户权限信息
        GetInfo({ commit, state }) {
            return new Promise((resolve, reject) => {
                getInfo(state.roleId).then(response => {
                    // const data = response.data.content;
                    // commit('SET_ROLES', data.role);
                    // commit('SET_NAME', data.upmsUser.realname);
                    // commit('SET_AVATAR', data.upmsUser.avatar);
                    // commit('SET_UID', data.uid);
                    // commit('SET_INTRODUCTION', data.introduction);
                    commit('SET_MENU_GENERATED', true);
                    resolve(response);
                }).catch(error => {
                    reject(error);
                });
            });
        },
        // 登出
        LogOut({ commit, state }) {
            return new Promise((resolve, reject) => {
                logout(state.token).then(() => {
                    commit('SET_UID', '');
                    commit('SET_TOKEN', '');
                    commit('SET_ROLES', []);
                    commit('SET_AGENTTYPE', []);
                    commit('SET_USERNAME', '');
                    commit('SET_REPORTUSERNAME', '');
                    Cookies.remove('agentType');
                    Cookies.remove('uid');
                    Cookies.remove('accessToken');
                    Cookies.remove('auserName');
                    Cookies.remove('reportUserName');
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },

        // 前端 登出
        FedLogOut({ commit }) {
            return new Promise(resolve => {
                commit('SET_UID', '');
                commit('SET_TOKEN', '');
                commit('SET_AGENTTYPE', []);
                commit('SET_USERNAME', '');
                Cookies.remove('uid');
                Cookies.remove('accessToken');
                Cookies.remove('agentType');
                Cookies.remove('userName');
                resolve();
            });
        },

        // 动态修改权限
        ChangeRole({ commit }, role) {
            return new Promise(resolve => {
                commit('SET_ROLES', [role]);
                commit('SET_TOKEN', role);
                Cookies.set('accessToken', role);
                resolve();
            })
        }

        // 后面可能会遇到切换身份的情况，暂存分发
        // this.$store.dispatch('SetRoleId', item.roleId);
        // SetRoleId({ commit }, roleId) {
        //     console.log('set..., roleId', roleId)
        //     Cookies.set('roleId', roleId);
        //     commit('SET_ROLEID', roleId);
        // },
    }
};

export default user;
